package com.shelpe.services.sinbad.utils;

import java.util.Collections;
import java.util.Comparator;

import org.apache.commons.lang3.math.NumberUtils;

import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.Campaign.RangeObject;
import com.shelpe.services.sinbad.entity.jpa.Campaign.ScheduleObject;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class CampaignUtils {
	public final static String WHOLE_DAY = "00:00-24:00:100;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100;00:00-24:00:100";

	public static String fromArea(Campaign.Area attribute) {
		StringBuffer buffer = new StringBuffer();
		if (attribute != null) {
			if (attribute.isAsAll()) {
				buffer.append("all");
			} else if (attribute.getNums() != null) {
				for (int n : attribute.getNums()) {
					buffer.append(n);
					buffer.append(",");
				}
			}
		}
		return (buffer.length() > 0 && buffer.lastIndexOf(",") == buffer.length() - 1)
				? buffer.substring(0, buffer.length() - 1) : buffer.toString();
	}

	public static Campaign.Area toArea(String dbData) {

		Campaign.Area a = new Campaign.Area();
		if (dbData != null) {
			if ("all".equals(dbData)) {
				a.setAsAll(true);
			} else {
				String[] numStrs = dbData.split(",");
				for (String n : numStrs) {
					if (NumberUtils.isNumber(n)) {
						a.addArea(NumberUtils.toInt(n));
					}
				}
			}
		}
		return a;
	}

	public static String fromScheduleObject(Campaign.ScheduleObject attribute) {
		String ret = null;
		if (attribute != null) {
			if (attribute.isAsAll()) {
				ret = "all";
			} else if (attribute.getRanges() != null) {
				StringBuffer buffer = new StringBuffer();
				Collections.sort(attribute.getRanges(), new Comparator<RangeObject>() {
					@Override
					public int compare(RangeObject o1, RangeObject o2) {
						return o1.getDayOfWeek() - o2.getDayOfWeek();
					}
				});
				int postDays = 1;
				for (int i = 0; i < attribute.getRanges().size(); i++) {
					RangeObject r = attribute.getRanges().get(i);
					if ((i + 1) < attribute.getRanges().size()) {
						postDays = attribute.getRanges().get(i + 1).getDayOfWeek();
					} else {
						postDays = 8;
					}
					buffer.append(r.getFrom());
					buffer.append("-");
					buffer.append(r.getTo());
					buffer.append(":");
					buffer.append(r.getDiscount());
					if (r.getDayOfWeek() == postDays) {
						buffer.append(",");
					} else {
						buffer.append(";");
					}
				}
				ret = buffer.substring(0, buffer.lastIndexOf(";"));
				log.info("ObjectSchedule to String:{}", ret);
			}
		}
		return ret;
	}

	public static ScheduleObject toScheduleObject(String dbData) {
		Campaign.ScheduleObject s = new Campaign.ScheduleObject();
		if (dbData != null) {
			if ("all".equals(dbData)) {
				s.setAsAll(true);
				dbData = WHOLE_DAY;
			}
			String[] rangeStrs = dbData.split(";");
			// 00:00-08:00:100,08:00-24:00:85;0;0;0;0;0;0
			int index = 1;
			for (String r : rangeStrs) {
				if (r.indexOf(":") > -1) {
					String[] rangeStrss = r.split(",");
					for (String rr : rangeStrss) {
						String from = rr.substring(0, rr.indexOf("-")); // from的形式:HH:mm
						String to = rr.substring(rr.indexOf("-") + 1, rr.lastIndexOf(":")); // to的形式:HH:mm
						String discount = rr.substring(rr.lastIndexOf(":") + 1); // discount的形式：int类型
						RangeObject range = new RangeObject();
						range.setDayOfWeek(index);
						range.setFrom(from);
						range.setTo(to);

						range.setDiscount(NumberUtils.toInt(discount));
						s.addRange(range);
					}
				} else {
					RangeObject range = new RangeObject();
					range.setDayOfWeek(index);
					range.setFrom("00:00");
					range.setTo("24:00");

					range.setDiscount(0);
					s.addRange(range);
				}
				index = index + 1;
			}
		}
		return s;
	}
}
